<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" type="text/css" href="public/css.css"> 
<?php
require_once './model/db3.php';

$query = "SELECT * FROM pages LIMIT 20";
$pages = getPages($query);
$keyword = NULL;
$ratingMin = NULL;
$ratingMax = NULL;
?>

<form method="POST">
    Keyword  : <input type="text" name="keyword"><br>
    Company  :  <select>
        <?php foreach ($pages as $page): ?>
            <option><?php echo $page["id"] ?></option>
        <?php endforeach; ?>
    </select> <br>
    Min fans &nbsp;: <input type="text" name="ratingMin"><br>
    Max fans : <input type="text" name="ratingMax"><br>
    <input type="submit" name="submit" value="Search">
</form>
<?php if (isset($_POST['submit'])): ?>
    <?php $keyword = $_POST['keyword']; ?>
    <?php $ratingMin = (double) $_POST['ratingMin']; ?>
    <?php $ratingMax = (double) $_POST['ratingMax']; ?>
<?php endif; ?>

<table id="table1" border="1px">
    <tr class="khung">
        <td class="pid">STT </td>
        <td class="id">id </td>
        <td class="name">name </td>
        <td class="likes">likes </td>
        <td class="talking">talking</td>
        <td class="category">category </td>
        <td class="link">link </td>
        <td class="website">website </td>
        <td class="picture">picture </td>
        <td class="country">country </td>
    </tr>
    <?php foreach ($pages as $page): ?>
        <?php if (($keyword==$page["name"]) && empty($ratingMin) && empty($ratingMax)): ?>
            <tr>
                <td><?php echo $page["pid"] ?></td>
                <td><?php echo $page["id"] ?></td>
                <td><?php echo $page["name"] ?></td>
                <td><?php echo $page["likes"] ?></td>
                <td><?php echo $page["talking_about_count"] ?></td>
                <td><?php echo $page["category"] ?></td>
                <td><a href='<?php echo $page["link"] ?>'><?php echo $page["link"] ?></td>
                <td><a href='<?php echo $page["website"] ?>'><?php echo $page["website"] ?></td>
                <td><img src ='<?php echo $page["picture"] ?>'</td>
                <td><?php echo $page["country"] ?></td>
            </tr>
        <?php elseif (empty($keyword) && empty($ratingMax)): ?>
            <?php if ($ratingMin <= (double) $page["likes"]): ?>
                <tr>
                    <td><?php echo $page["pid"] ?></td>
                    <td><?php echo $page["id"] ?></td>
                    <td><?php echo $page["name"] ?></td>
                    <td><?php echo $page["likes"] ?></td>
                    <td><?php echo $page["talking_about_count"] ?></td>
                    <td><?php echo $page["category"] ?></td>
                    <td><a href='<?php echo $page["link"] ?>'><?php echo $page["link"] ?></td>
                    <td><a href='<?php echo $page["website"] ?>'><?php echo $page["website"] ?></td>
                    <td><img src ='<?php echo $page["picture"] ?>'</td>
                    <td><?php echo $page["country"] ?></td>
                </tr>
            <?php endif; ?>
        <?php else: ?>
            <?php if (($ratingMin <= (double) $page["likes"]) && ($ratingMax >= $page["likes"])): ?>
                <tr>
                    <td><?php echo $page["pid"] ?></td>
                    <td><?php echo $page["id"] ?></td>
                    <td><?php echo $page["name"] ?></td>
                    <td><?php echo $page["likes"] ?></td>
                    <td><?php echo $page["talking_about_count"] ?></td>
                    <td><?php echo $page["category"] ?></td>
                    <td><a href='<?php echo $page["link"] ?>'><?php echo $page["link"] ?></td>
                    <td><a href='<?php echo $page["website"] ?>'><?php echo $page["website"] ?></td>
                    <td><img src ='<?php echo $page["picture"] ?>'</td>
                    <td><?php echo $page["country"] ?></td>
                </tr>
            <?php endif; ?>
        <?php endif; ?>
    <?php endforeach; ?>
</table>
?>
